package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.domain.DepartmentDomain;
import com.ngc.mall.mapper.DepartmentMapper;
import com.ngc.mall.service.IDepartmentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * 部门实现类
 * @author: chenxiaolin
 * @date 2023/10/28 22:48
**/
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<DepartmentMapper, DepartmentDomain> implements IDepartmentService {
    @Override
    public Long createDepartment(DepartmentDomain domain) {
        return super.create(domain);
    }

    @Override
    public Boolean deleteById(Long id) {
        List<Long> ids=new ArrayList<>();
        List<Long> childrenIds =super.list(Wrappers.<DepartmentDomain>lambdaQuery().eq(DepartmentDomain::getPid,id)).stream().map(CategoryDomain->CategoryDomain.getId()).collect(Collectors.toList());
        ids.add(id);
        ids.addAll(childrenIds);
        return super.removeByIds(ids);
    }

    @Override
    public Boolean updateById(Long id, DepartmentDomain domain) {
        return super.update(domain, Wrappers.<DepartmentDomain>lambdaQuery().eq(DepartmentDomain::getId,id));
    }

    @Override
    public List<DepartmentDomain> findAll() {
        return super.list();
    }

    @Override
    public List<DepartmentDomain> findAllEnabled() {
        return super.list(Wrappers.<DepartmentDomain>lambdaQuery().eq(DepartmentDomain::getIsEnabled,true));
    }


}
